import cv2
from draw_zoom import DrawZoom
import os,sys
from threading import Thread
import numpy as np

capture = cv2.VideoCapture(0)
 while True:
     ret, image = capture.read()
     width = capture.get(3)
     height = capture.get(4)
     #放大棋盘
     draw_zoom = draw_zoomer.update_image(image, (0, 255, 0), (360, 268))

     #cv2.namedWindow(WIN_NAME_ZOOM, 0)
     # 变为灰度图
     gray = cv2.cvtColor(draw_zoom, cv2.COLOR_BGR2GRAY)

     gray = cv2.medianBlur(gray, 5)

     ret, threshold = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

     cv2.imshow('threshold', threshold)

     # 级联分类器
     detector = cv2.CascadeClassifier("qipanxin.xml")
     rects = detector.detectMultiScale(draw_zoom, minNeighbors=3, minSize=(10, 10), maxSize=(21, 21),
                                       flags=cv2.CASCADE_SCALE_IMAGE)
     for (x, y, w, h) in rects:
         height = y + h / 2
         width = x + w / 2
         if threshold[int(height),int(width) ] == 255:
             continue

         # 画矩形框
         cv2.rectangle(draw_zoom, (x, y), (x + w, y + h), (0, 255, 0), 2)
         # 棋子识别
         face = cv2.resize(gray[y:y + h, x:x + w], (21, 21))
         # 预测棋子
         params = model.predict(face)
     cv2.imshow(WIN_NAME_BIG, draw_zoom)

     key = cv2.waitKey(1)
     if key == 27:  # ESC
        break
cv2.destroyAllWindows()